﻿using System;
using System.Collections.Generic;
using Sunny.UI;

// ReSharper disable once CheckNamespace
namespace SmartMotion.Core
{
    public class PagesMgr
    {
        #region 属性单例模式

        private static readonly Lazy<PagesMgr> Instance = new Lazy<PagesMgr>(() => new PagesMgr());

        private PagesMgr()
        {
            _panels = new Dictionary<string, UIPanel>();
            _pages = new Dictionary<string, Dictionary<string, UIPage>>();

        }
        public static PagesMgr Ins { get; } = Instance.Value;

        #endregion
        /// <summary>
        /// 所有容器
        /// </summary>
        private readonly Dictionary<string, UIPanel> _panels;
        /// <summary>
        /// 所有页面
        /// </summary>
        private readonly Dictionary<string, Dictionary<string, UIPage>> _pages;

        /// <summary>
        /// 注册容器
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="panelName"></param>
        /// <returns></returns>
        public bool RegisterPanel(UIPanel panel, string panelName = "default")
        {
            if (panelName == "default") panelName = panel.Name;
            if (_panels.ContainsKey(panelName)) return false;
            _panels.Add(panelName,panel);
            return true;
        }

        public bool RegisterPageToPanel(string panelName, UIPage page, string pageName = "default")
        {
            if (!_panels.ContainsKey(panelName)) return false;
            if (!_pages.ContainsKey(panelName)) _pages.Add(panelName, new Dictionary<string, UIPage>());
            if (pageName == "default") pageName = page.Name;
            if (_pages[panelName].ContainsKey(pageName)) return false;
            _pages[panelName].Add(pageName,page);
            return true;
        }

        public bool ShowPage(string panelName, string pageName)
        {
            if (!_panels.ContainsKey(panelName)) return false;
            if (!_pages.ContainsKey(panelName)) return false;
            if (!_pages[panelName].ContainsKey(pageName)) return false;
            var panel = _panels[panelName];
            var page = _pages[panelName][pageName];
            panel.Controls.Clear();
            panel.Controls.Add(page);
            page.Show();
            return true;
        }
    }
}
    